package item;

import effects.Effect;
import entity.Entity.InteractVisitor;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;

public class Trap extends InteractiveItem {

    private boolean visible;
    
    public Trap(String name, Effect effect) {
        super(name, effect);
    }
    
    public Trap(String name, Requirements requirements) {
        super(name, requirements);
    }
    
    /**
     * have the visitor visit this item to interact with it
     */
    public void accept(InteractVisitor iv) {
    	iv.visitTrap(this);
    }
    
    /**
     * let people walk onto traps
     */
    public boolean canBeWalkedOn() {
        return true;
    }
    
    /**
     * accessor for the visibility boolean
     * @return
     */
    public boolean isVisible() {
    	return visible;
    }
    
    /**
     * mutator for the visibility boolean
     * @param value
     */
    public void setVisible(boolean value) {
    	visible = value;
    }
    
    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                LoadSaveObject lso = Trap.super.getMemento().getState();
                Iterator<LoadSaveObject> i = lso.getIterator();
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Trap");
                
                for (i.init(); i.hasNext(); i.next()) {
                    toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
                }
                
                toReturn.add("visible", new LoadSavePrimitive("visible", "boolean", String.valueOf(visible)));
                
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
                Trap.super.getMemento().restoreState(lso);
                visible = Boolean.valueOf(lso.get("visible").getValue());
                
            }
            
        };
       
    }

    public Trap() { }
    
    @Override
	public Trap clone() {
		Trap toReturn = new Trap();
		toReturn.getMemento().restoreState(this.getMemento().getState());
		return toReturn;
	}

}
